var appCategory=[];
var allapplist=[];
var installAppList=[];
var progressAppList=[];
var appCategoryList=[];
//var searchAppList=[];

function appCategorys(sortId,sortName,imgurl,appCount) {
    this.sortId=sortId;
    this.sortName=sortName;
    this.imgurl=imgurl;
    this.appCount=appCount;
}

function apps(id,name,lmg,apk,downloads,softwaresize,updatetime,packagename,version,superscript,classname)
{
    this.id=id;
    this.name=name;
    this.lmg=lmg;
    this.apk=apk;
    this.downloads=downloads;
    this.softwaresize=softwaresize;
    this.updatetime=updatetime;
    this.packagename=packagename;
    this.version=version;
    this.superscript=superscript;
    this.classname=classname;
}

function installApps(packageName,className,label,versionName) {
    this.packageName=packageName;
    this.className=className;
    this.label=label;
    this.versionName=versionName;
}

function progressApps(packageName,progress) {
    this.packageName=packageName;
    this.progress=progress;
}

function apkinfo(isApk,name,version,pkgName,size,icon,error_code,error_msg) {
    this.isApk=isApk;
    this.name=name;
    this.version=version;
    this.pkgName=pkgName;
    this.size=size;
    this.icon=icon;
    this.error_code=error_code;
    this.error_msg=error_msg;
}

function json_to_appCategory(data){
    var categoryList=[];
   //var data_t = eval("("+data+")");
   var data_t = data;
   if (data_t.data==""){
       alert("app分类数据为空");
       return ;
    }

    var data_t = data_t.data;
    var j = 0;
    if (data_t.length==0){
       alert("app分类数据为空");
       return ;
    }
    for (var x=0;x<data_t.length; x++) {
        var sortId=data_t[x].sortId;
        var sortName=data_t[x].sortName;
        var imgurl=data_t[x].imgurl;
        var appCount=data_t[x].appCount;
//        alert("22 json_to_appCategory data is " + sortName);
        var category=new appCategorys(sortId,sortName,imgurl,appCount);
        categoryList[j]=category;
        j++;
    }
    appCategoryList = categoryList;
    for(var i=0; i<4; i++){
        var tabname="tab"+i;
//        alert(appCategoryList.length+";tabname:"+tabname+";"+appCategoryList[i].sortId);
        document.getElementById(tabname).innerHTML = appCategoryList[i].sortName;
        getAppList(i,appCategoryList[i].sortId,1);
	}
}

function json_to_applist(type,data){
    var applist=[];
//    var data_t = eval("("+data+")");
   var data_t = data;
    if (data_t.data==""){
       alert("数据为空");
       return ;
    }
    //var data_t = $.parseJSON(data_t.data);
    //alert(2+data_t);
    var data_t = data_t.data;
    var j = 0;
    for (var x=0;x<data_t.length; x++) {
         var id=data_t[x].id;
         var name=data_t[x].name;
         var lmg=data_t[x].lmg;
         var apk=data_t[x].apk;
         var downloads=data_t[x].downloads;
         var softwaresize=data_t[x].softwaresize;
         var updatetime=data_t[x].updatetime;
         var packagename=data_t[x].packagename;
         var version=data_t[x].version;
         var superscript=data_t[x].superscript;
         var app=new apps(id,name,lmg,apk,downloads,softwaresize,updatetime,packagename,version,superscript,"");
         applist[j]=app;
         j++;
    }

   var templist = [];
   var templist2 = [];
   if(allapplist[type] != null){
       templist = allapplist[type];
       templist2 = templist.concat(applist);
   }else{
        templist2 = applist;
   }
   allapplist[type] = templist2;
   return applist;
}

function json_to_installAppList(data){
   var data_t = eval("("+data+")");
   if (data_t.data==""){
       alert("数据为空");
       return ;
    }
    var data_t = data_t.data;
    var j = 0;
    for (var x=0;x<data_t.length; x++) {
        var packageName=data_t[x].packageName;
        var className=data_t[x].className;
        var label=data_t[x].label;
        var versionName=data_t[x].versionName;
        //alert("22 json_to_installAppList data is " + packageName + data_t[x].label);
        var installapp=new installApps(packageName,className,label,versionName);
        installAppList[j]=installapp;
        j++;
    }
}

function json_to_getAllProgress(data){
   progressAppList = [];
    var data_t = eval("("+data+")");
   if (data_t.data=="" || data_t.data == null){
            var progressAppTest = new progressApps("1","1")	;
            progressAppList.push(progressAppTest);
           return ;
    }
     var data_t = data_t.data;
      for (var x=0;x<data_t.length; x++) {
             var packageName=data_t[x].packageName;
             var progress=data_t[x].progress;
             var progressApp = new progressApps(packageName,progress)	;
             progressAppList.push(progressApp);
       }
       var progressAppTest = new progressApps("1","1")	;
       progressAppList.push(progressAppTest);
}

function json_to_getApkInfo(data){
        var data_t = eval("("+data+")");
       if (data_t==""){
               alert("数据为空");
                 return ;
        }

        var isAPK,name,version,pkgName,size,icon,error_code,error_msg;
        if(data_t.error_code != 0) {
             error_code=data_t.error_code;
             error_msg=data_t.error_msg;
             var appinfo = new apkinfo(isAPK,name,version,pkgName,size,icon,error_code,error_msg);
             return appinfo;
        }

         isAPK=data_t.data.isAPK;
         name=data_t.data.name;
         version=data_t.data.version;
         pkgName=data_t.data.pkgName;
         size=data_t.data.size;
         icon=data_t.data.icon;
         error_code=data_t.error_code;
         error_msg = "";
         var appinfo =new apkinfo (isAPK,name,version,pkgName,size,icon,error_code,error_msg);
         return appinfo;
}

function json_to_progress(data){
    var data_t = eval("("+data+")");
   if (data_t==""){
           alert("数据为空");
             return ;
    }
    if(data_t.progress==null ||data_t.progress==""){
            return;
    }

    var progress = data_t.progress.progress;
     return progress;
}

function json_to_search(data){
    var searchAppList=[];
//var data_t = eval("("+data+")");
    var data_t = data;
   if (data_t==""){
           alert("数据为空");
             return false;
    }
    if(data_t.count==null ||data_t.count==0){
      // allapplist[3]=[];
        return false;
    }
     var data_t = data_t.list;
    if(data_t==null ||data_t==""){
      // allapplist[3]=[];
    return false;
    }
     var j = 0;
      for (var x=0;x<data_t.length; x++) {
             var downloads=data_t[x].downloads;
             var id=data_t[x].id;
             var lmg=data_t[x].lmg;
             var name=data_t[x].name;
             var packagename=data_t[x].packagename;
             var softwaresize=data_t[x].softwaresize;
             var apk,updatetime,version,superscript;
             var app=new apps(id,name,lmg,apk,downloads,softwaresize,updatetime,packagename,version,superscript,"");
             searchAppList[j]=app;
             j++;
       }
    allapplist[4]=searchAppList;
    return true;
}

